<?php

namespace app\admin\model\user\money;

use think\Model;

/**
 * 用户积分变动记录模型
 */
class Log extends Model
{
    // 表名
    protected $name = 'user_money_log';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 积分类型定义
    const TYPE_SHOPPING_INTEGRAL = '购物积分';     // 购物积分
    const TYPE_COUPON_INTEGRAL = '消费券';         // 消费券
    const TYPE_EQUITY_VALUE = '权益值';               // 权益值
    const TYPE_PROMOTION_BONUS = '推广红包';         // 推广红包
    const TYPE_GREEN_INTEGRAL = '绿色积分';           // 绿色积分
    // const TYPE_SHOPPING_INTEGRAL = 'shopping_integral';     // 购物积分
    // const TYPE_COUPON_INTEGRAL = 'coupon_integral';         // 消费券
    // const TYPE_EQUITY_VALUE = 'equity_value';               // 权益值
    // const TYPE_PROMOTION_BONUS = 'promotion_bonus';         // 推广红包
    // const TYPE_GREEN_INTEGRAL = 'green_integral';           // 绿色积分
    
    
    // 变动类型定义
    const BIAN_TYPE_INCREASE = '增加';                  // 增加 increase
    const BIAN_TYPE_DECREASE = '减少';                  // 减少 decrease

    // 追加属性
    protected $append = [
        'type_text',
        'bian_type_text',
    ];
    
    /**
     * 获取积分类型列表
     */
    public function getTypeList()
    {
        return [
            self::TYPE_SHOPPING_INTEGRAL => '购物积分',
            self::TYPE_COUPON_INTEGRAL => '消费券',
            self::TYPE_EQUITY_VALUE => '权益值',
            self::TYPE_PROMOTION_BONUS => '推广红包',
            self::TYPE_GREEN_INTEGRAL => '绿色积分'
        ];
    }
    
    /**
     * 获取变动类型列表
     */
    public function getBianTypeList()
    {
        return [
            self::BIAN_TYPE_INCREASE => '增加',
            self::BIAN_TYPE_DECREASE => '减少'
        ];
    }
    
    /**
     * 记录积分变动
     * @param int $userId 用户ID
     * @param string $type 积分类型
     * @param float $changeAmount 变动金额（正数为增加，负数为减少）
     * @param float $beforeValue 变动前的值
     * @param float $afterValue 变动后的值
     * @param string $memo 备注
     * @param string $type2 第二种类型（可选）
     * @return bool|\think\Model
     */
    public static function recordChange($userId, $type, $changeAmount, $beforeValue, $afterValue, $memo = '', $type2 = '')
    {
        try {
            $bianType = $changeAmount >= 0 ? self::BIAN_TYPE_INCREASE : self::BIAN_TYPE_DECREASE;
            $money = abs($changeAmount);
            
            // 正数带有"+"符号保存
            if ($changeAmount > 0) {
                $bianMoneyValue = '+' . $changeAmount;
            } else {
                $bianMoneyValue = $changeAmount;
            }
            
            $data = [
                'user_id' => $userId,
                'type' => $type,
                'bian_type' => $bianType,
                'bian_money' => $bianMoneyValue,
                'money' => $money,
                'before' => $beforeValue,
                'after' => $afterValue,
                'memo' => $memo
            ];
            
            // 如果有type2字段，则添加
            if (!empty($type2)) {
                $data['type2'] = $type2;
            }
            
            return self::create($data);
        } catch (\Exception $e) {
            return false;
        }
    }
    
    /**
     * 记录购物积分变动
     */
    public static function recordShoppingIntegral($userId, $changeAmount, $beforeValue, $afterValue, $memo = '')
    {
        return self::recordChange($userId, self::TYPE_SHOPPING_INTEGRAL, $changeAmount, $beforeValue, $afterValue, $memo);
    }
    
    /**
     * 记录消费券变动
     */
    public static function recordCouponIntegral($userId, $changeAmount, $beforeValue, $afterValue, $memo = '')
    {
        return self::recordChange($userId, self::TYPE_COUPON_INTEGRAL, $changeAmount, $beforeValue, $afterValue, $memo);
    }
    
    /**
     * 记录权益值变动
     */
    public static function recordEquityValue($userId, $changeAmount, $beforeValue, $afterValue, $memo = '')
    {
        return self::recordChange($userId, self::TYPE_EQUITY_VALUE, $changeAmount, $beforeValue, $afterValue, $memo);
    }
    
    /**
     * 记录推广奖励权益值变动（带type2字段）
     * @param int $userId 用户ID
     * @param float $changeAmount 变动金额
     * @param float $beforeValue 变动前的值
     * @param float $afterValue 变动后的值
     * @param string $memo 备注
     * @return bool|\think\Model
     */
    public static function recordPromotionEquityValue($userId, $changeAmount, $beforeValue, $afterValue, $memo = '')
    {
        return self::recordChange($userId, self::TYPE_EQUITY_VALUE, $changeAmount, $beforeValue, $afterValue, $memo, self::TYPE_PROMOTION_BONUS);
    }

    /**
     * 记录团队奖励权益值 变动（带type2字段）
     * @param int $userId 用户ID
     * @param float $changeAmount 变动金额
     * @param float $beforeValue 变动前的值
     * @param float $afterValue 变动后的值
     * @param string $memo 备注
     * @return bool|\think\Model
     */
    public static function recordTeamEquityValue($userId, $changeAmount, $beforeValue, $afterValue, $memo = '')
    {
        return self::recordChange($userId, self::TYPE_EQUITY_VALUE, $changeAmount, $beforeValue, $afterValue, $memo, self::TYPE_GREEN_INTEGRAL);
    }
    
    /**
     * 记录推广红包变动
     */
    // public static function recordPromotionBonus($userId, $changeAmount, $beforeValue, $afterValue, $memo = '')
    // {
    //     return self::recordChange($userId, self::TYPE_PROMOTION_BONUS, $changeAmount, $beforeValue, $afterValue, $memo);
    // }
    
    /**
     * 记录绿色积分变动
     */
    // public static function recordGreenIntegral($userId, $changeAmount, $beforeValue, $afterValue, $memo = '')
    // {
    //     return self::recordChange($userId, self::TYPE_GREEN_INTEGRAL, $changeAmount, $beforeValue, $afterValue, $memo);
    // }
    
    /**
     * type字段获取器
     */
    public function getTypeTextAttr($value, $data)
    {
        $typeList = $this->getTypeList();
        return isset($typeList[$data['type']]) ? $typeList[$data['type']] : $data['type'];
    }
    
    /**
     * bian_type字段获取器
     */
    public function getBianTypeTextAttr($value, $data)
    {
        $bianTypeList = $this->getBianTypeList();
        return isset($bianTypeList[$data['bian_type']]) ? $bianTypeList[$data['bian_type']] : $data['bian_type'];
    }
    
    
    // 关联用户模型
    public function user()
    {
        return $this->belongsTo('app\\admin\\model\\User', 'user_id');
    }
}
